package br.com.checkandgo;
import java.util.ArrayList;
import java.util.List;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import br.com.checkandgo.R;
import br.com.checkandgo.model.Constants;
import br.com.checkandgo.model.RotasCarona;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class ListaCaronasActivity extends Activity{
	public ListView listCaronas;
	ArrayList<RotasCarona> ListaCaronas = new ArrayList<RotasCarona>();
	ArrayList<String> arrayLista = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.lista_caronas);
		listCaronas = (ListView) findViewById(R.id.listView1);

		ParseQuery<ParseObject> query = ParseQuery.getQuery("PontosRota");
		query.findInBackground(new FindCallback<ParseObject>() {
			public void done(List<ParseObject> rotasList, ParseException e) {
				for(ParseObject obj : rotasList){

					List<Double> lats = obj.getList("latitude");
					List<Double> longs = obj.getList("longitude");

					boolean posIni = false;
					boolean posFim = false;

					for (int i = 0; i < lats.size(); i++) {

						if (distFrom(lats.get(i), longs.get(i), Constants.getFirstPoint().latitude, Constants.getFirstPoint().longitude)) {
							posIni = true;

						} else if(distFrom(lats.get(i), longs.get(i), Constants.getLastPoint().latitude, Constants.getLastPoint().longitude)) {
							posFim = true;
						}
					}

					if (posIni && posFim) {
						final RotasCarona rotasCarona = new RotasCarona();

						ParseQuery<ParseObject> query = ParseQuery.getQuery("Rota");
						query.whereEqualTo("objectid", obj.getString("idRota"));
						query.findInBackground(new FindCallback<ParseObject>() {
							public void done(List<ParseObject> rotasList, ParseException e) {
								ParseObject rota = rotasList.get(0);
								rotasCarona.setNome(rota.getString("nome"));
								rotasCarona.setDia(rota.getString("dia"));		

								ParseQuery<ParseObject> queryUser = ParseQuery.getQuery("User");
								queryUser.whereEqualTo("objectid", rota.getString("idUser"));
								queryUser.findInBackground(new FindCallback<ParseObject>() {
									public void done(List<ParseObject> rotasList, ParseException e) {
										ParseObject rota = rotasList.get(0);
										rotasCarona.setIdUsuario(rota.getString("idUser"));
										rotasCarona.setEmailUsuario(rota.getString("email"));							
									}
								});

							}
						});
						ListaCaronas.add(rotasCarona);
					}

				}

			}

		});
		for(RotasCarona rc : ListaCaronas){
			String texto = "nome :" + rc.getNome() +
							"dia :" + rc.getDia() +
							"Caronante :" + rc.getEmailUsuario();
			arrayLista.add(texto);
		}
		ArrayAdapter<String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayLista);
		listCaronas.setAdapter(adapter);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.conta, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			Intent intent = new Intent(ListaCaronasActivity.this, ContaActivity.class);
			startActivity(intent);
			return true;
		}
		if (id == R.id.home) {
			Intent intent = new Intent(ListaCaronasActivity.this, MenuPrincipalActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public boolean distFrom(double latitude, double longitude, double latitude2, double longitude2) {
		double earthRadius = 3958.75;
		double dLat = Math.toRadians(latitude2-latitude);
		double dLng = Math.toRadians(longitude2-longitude);
		double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
				Math.cos(Math.toRadians(latitude)) * Math.cos(Math.toRadians(latitude2)) *
				Math.sin(dLng/2) * Math.sin(dLng/2);
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
		double dist = earthRadius * c;

		int meterConversion = 1609;

		float result = (float) (dist * meterConversion);
		if(result < 200){
			return true;
		} else {
			return false;
		}
	}

}
